package com.framework.component.resolver.register;

import com.framework.component.resolver.ValueResolver;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class ResolverBootstrap implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private ResolverRegister resolverRegister;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void init() {
        Map<String, ValueResolver> valueResolverBeans = applicationContext.getBeansOfType(ValueResolver.class);
        valueResolverBeans.values().forEach(
                resolver -> resolverRegister.register(resolver)
        );
    }
}
